<template>
  <el-main>
    <Navbar></Navbar>
    <!--  条件栏  -->
    <el-row>
      <el-col :span="24">
        <el-input v-model="queryPageBean.queryString" placeholder="名称/关键字" style="width: 200px;"></el-input>
        <el-button @click="findPage(queryPageBean)">查询</el-button>
        <el-button type="primary" @click="handleAddPage()">新建</el-button>
      </el-col>
    </el-row>
    <!--    表格-->
    <Table :table-data="page.records" :table-head="tableHead" :operation="operation"></Table>
    <!--    分页组件-->
    <el-pagination
        align="right"
        @current-change="handleCurrentChange"
        :current-page="queryPageBean.currentPage"
        :page-size="queryPageBean.pageSize"
        layout="total, prev, pager, next, jumper"
        :total="page.total">
    </el-pagination>
    <!--    新建角色的页面-->
    <el-dialog title="新建角色" :visible.sync="dialogFormVisible">
      <!--   tag开始   -->
      <el-tabs value="first" type="card">
        <el-tab-pane label="基本信息" name="first">
          <el-form :model="formData" label-position="top">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="名称" prop="name">
                  <el-input v-model="formData.name"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="关键字" prop="keyword">
                  <el-input v-model="formData.keyword"/>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="描述">
                  <el-input v-model="formData.description" type="textarea"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-tab-pane>
        <!--   菜单     -->
        <el-tab-pane label="所含菜单" name="second">
          <el-row>
            <el-col :span="24">
              <el-tree ref="tree"
                       :data="allMenuTree"
                       :props="menuDefaultProps"
                       :default-expanded-keys="[0]"
                       node-key="id"
                       show-checkbox>
              </el-tree>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAdd">确 定</el-button>
      </div>
    </el-dialog>
<!--  编辑角色的页面  -->
    <el-dialog title="编辑角色" :visible.sync="dialogFormVisibleEdit">
      <!--   tag开始   -->
      <el-tabs value="first" type="card">
        <el-tab-pane label="基本信息" name="first">
          <el-form :model="formData" label-position="top">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="名称" prop="name">
                  <el-input v-model="formData.name"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="关键字" prop="keyword">
                  <el-input v-model="formData.keyword"/>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="描述">
                  <el-input v-model="formData.description" type="textarea"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-tab-pane>
        <!--   菜单     -->
        <el-tab-pane label="所含菜单" name="second">
          <el-row>
            <el-col :span="24">
              <el-tree ref="tree"
                       :data="allMenuTree"
                       :props="menuDefaultProps"
                       :default-expanded-keys="[0]"
                       :default-checked-keys="formData.menuIds"
                       node-key="id"
                       show-checkbox>
              </el-tree>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleEdit = false">取 消</el-button>
        <el-button type="primary" @click="handleEdit">确 定</el-button>
      </div>
    </el-dialog>
  </el-main>
</template>

<script>
import Table from "@/components/Table.vue"
import { getRoles, addRole, editRole, getAllMenusToTree, getMenuIdsByRoleId } from "@/api"

export default {
  data() {
    return {
      // 设置树形菜单对应属性
      menuDefaultProps: {
        // 子节点的属性名称
        children: 'children',
        // 显示的名字
        label: 'title'
      },
      allMenuTree: [], // 树形结构的菜单
      tableHead: [],  // 表头
      operation: [],  // 操作
      queryPageBean: {  // 分页查询条件
        currentPage: 1, // 当前页
        pageSize: 10,   // 每页记录数
        queryString: '',// 查询条件
      },
      dialogFormVisible: false, // 新增框显示
      dialogFormVisibleEdit: false, // 编辑框显示
      formData: { // 表单数据
        menuIds: [], // 绑定的菜单ID
      },
      page: {}, // 分页对象
    }
  },
  watch: {
    // 监听查询条件变化
    'queryPageBean.queryString': {
      handler(newValue, oldValue) {
        this.queryPageBean.currentPage = 1 // 将页数设为1
      },
      immediate: true
    },
  },
  methods: {
    // 编辑角色
    handleEdit() {
      // 将选中的菜单赋值到表单的menuIds中
      this.formData.menuIds = this.$refs.tree.getCheckedKeys().filter(element => element !== 0)
      editRole(this.formData).then((res) => {
        if (res.data.flag) {
          this.$message.success(res.data.message)
          this.formData = {}          // 清空表单数据
          this.dialogFormVisibleEdit = false // 关闭编辑框
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    // 定义弹出新增页面需要做的事
    handleAddPage() {
      this.findAllMenusAsTree() // 查询所有的菜单（树形结构）
      this.dialogFormVisible = true
    },
    // 新增角色
    handleAdd() {
      // 将选中的菜单赋值到表单的menuIds中
      this.formData.menuIds = this.$refs.tree.getCheckedKeys().filter(element => element !== 0)
      // 发送请求
      addRole(this.formData).then((res) => {
        if (res.data.flag) {
          this.$message({
            message: res.data.message,
            type: 'success'
          })
          this.formData.menuIds = [] // 清空菜单ID
          this.findPage()
          this.dialogFormVisible = false
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    // 查询所有菜单（树形结构）
    findAllMenusAsTree() {
      getAllMenusToTree().then((res) => {
        if (res.data.flag) {
          this.allMenuTree = [{
            id: 0,
            title: "传智健康",
            parentMenuId: null,
            children: res.data.data
          }]
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    // 查询角色
    findPage() {
      getRoles(this.queryPageBean).then((res) => {
        this.page = res.data.data
      })
    },
    // 当前页发生变化
    handleCurrentChange(val) {
      this.queryPageBean.currentPage = val
      this.findPage()
    },
  }
  ,
  created() {
    // 自定义操作
    this.operation = [
      {
        type: 'primary',
        name: '编辑',
        action: (row) => {
          this.findAllMenusAsTree() // 查询所有的菜单（树形结构）
          getMenuIdsByRoleId({
            id: row.id
          }).then((res) => {
            this.formData = row // 回显本行数据
            this.formData.menuIds = res.data.data // 设置关联的菜单
            this.dialogFormVisibleEdit = true // 显示编辑框
          })
        },
        isDisabled: (row) => {
          return false
        }
      }, {
        type: 'danger',
        name: '删除',
        action: (row) => {
          console.log(row)
        },
        isDisabled: (row) => {
          return false
        }
      }
    ]
    // 自定义表头
    this.tableHead = [
      {
        name: '名称',
        prop: 'name'
      }, {
        name: '关键字',
        prop: 'keyword'
      }, {
        name: '描述',
        prop: 'description'
      },
    ]
    // 获取页面数据
    this.findPage()
  }
  ,
  components: {
    Table,
  }
}
</script>

<style lang="less">

</style>